package com.example.myapplication;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;

public class ClassroomInfo_Adapter extends BaseAdapter {
    private String Tag = "ClassroomInfo";
    private Context mContext;
    private LayoutInflater inflater;
    private String user_name ;


    String vacant = new String();
    String busy = new String();
    String applied = new String();

    //用于存放教室数据
    private ArrayList<classroom_info> classroom_info_local = new ArrayList<classroom_info>();




    /*
    获得空闲时间、被占用时间和当前用户的占用时间
     */
    private void get_Time_vacant_and_busy(String vacant,String busy,String applied,classroom_info info,String name){
        StringBuffer time_vacant = new StringBuffer("空闲：");
        StringBuffer time_busy = new StringBuffer("占用：");
        StringBuffer user_applied = new StringBuffer(" ");

        Log.d(Tag,"class1"+info.getClass1());
        Log.d(Tag,"class2"+info.getClass2());
        Log.d(Tag,"class3"+info.getClass3());
        Log.d(Tag,"class4"+info.getClass4());
        Log.d(Tag,"name "+name);

        if(info.getClass1().equals("空闲")){
            time_vacant.append("1 ");
        } else{
            time_busy.append("1-"+info.getClass1()+"    ");
            if(info.getClass1().equals(name)){
                Log.d("ClassroomInfo","查询到用户的申请");
                user_applied.append(info.getName_building()+" "+info.getId_classroom()+"-1");
            }
        }

        if(info.getClass2().equals("空闲")){
            time_vacant.append("2 ");
        } else{
            time_busy.append("2-"+info.getClass2()+"    ");
            if(info.getClass2().equals(name)){
                Log.d("ClassroomInfo","查询到用户的申请");
                user_applied.append(info.getName_building()+" "+info.getId_classroom()+"-2");
            }
        }
        if(info.getClass3().equals("空闲")){
            time_vacant.append("3 ");
        } else{
            time_busy.append("3-"+info.getClass3()+"    ");
            if(info.getClass3().equals(name)){
                Log.d("ClassroomInfo","查询到用户的申请");
                user_applied.append(info.getName_building()+" "+info.getId_classroom()+"-3");
            }
        }

        if(info.getClass4().equals("空闲")){
            time_vacant.append("4 ");
        } else{
            time_busy.append("4-"+info.getClass4()+"    ");
            if(info.getClass4().equals(name)){
                Log.d("ClassroomInfo","查询到用户的申请");
                user_applied.append(info.getName_building()+" "+info.getId_classroom()+"-4");
            }
        }

        this.vacant = time_vacant.toString();
        this.busy = time_busy.toString();
        applied = user_applied.toString();

        StudentActivity.setApplied(applied);
        Log.d("ClassroomInfo_Adapter","83行applied"+applied);


    }



    //构造函数，初始化
    public ClassroomInfo_Adapter(Context context, ArrayList<classroom_info> info,String user_name){
        mContext = context;
        classroom_info_local = info;
        this.user_name = user_name;
        Log.d("ClassroomInfo_Adapter", user_name);
        inflater =(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount() {
        // 有多少条数据
        return classroom_info_local.size();
    }

    @Override
    public Object getItem(int position) {  //position就是这条数据的位置
        //返回某一条数据,
        return classroom_info_local.get(position);
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    //返回某一条数据的视图，因为每一条数据都会有自己的视图
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        //空闲时间和被占用时间

        get_Time_vacant_and_busy(vacant,busy,applied,classroom_info_local.get(position),user_name);

        //返回一个视图，convertView 就是找到要绑定的视图，LayoutInfalter的主要功能就是解析View
        // 将Layout从xml文件从解析出来，解析成View，一个视图
        convertView= inflater.inflate(R.layout.classroom_view_layout,null);

        //获取控件，从视图中获取控件
        TextView name_building = (TextView) convertView.findViewById(R.id.cr_view_name_building); //教学楼名
        TextView id_classroom = (TextView) convertView.findViewById(R.id.cr_view_id_cr);//教室号
        TextView time_vacant = (TextView) convertView.findViewById(R.id.cr_view_time_vacant);//空闲时间
        TextView time_busy = (TextView) convertView.findViewById(R.id.cr_view_time_busy); //被占用时间




        //和数据之间进行绑定
        name_building.setText(classroom_info_local.get(position).getName_building());
        id_classroom.setText(classroom_info_local.get(position).getId_classroom());
        time_vacant.setText(vacant);
        time_busy.setText(busy);


        return convertView;  //返回一个视图
    }
}
